﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {

        private void UBTVK04EN(DataSet db)
        {
            try
            {
                DataTable tbVKKOPF = dsSource.Tables["VKKOPF"];
                DataRow dwVKKOPF = tbVKKOPF.Rows[0];
                DataTable tbVKPOS = dsSource.Tables["VKPOS"];
                DataRow dwVKPOS = tbVKPOS.Rows[0];
                DataTable tbKDSTAMM = dsSource.Tables["KDSTAMM"];
                DataRow dwKDSTAMM = tbKDSTAMM.Rows[0];
                DataTable tbWASTAMM = dsSource.Tables["WASTAMM"];
                string src = ""; double fzp = 0;
                if (tbWASTAMM.Rows.Count > 0)
                {
                    DataRow dwWASTAMM = tbWASTAMM.Rows[0];
                    fzp = Convert.ToDouble(dwWASTAMM["WAFSTDLG"].ToString().Trim()) * 0.001;
                    src = "M";
                    exAgent.SetCellValue(11, 15, fzp + src);
                    fzp = Convert.ToDouble(dwWASTAMM["WAFGRLFD"].ToString().Trim());
                    src = "克/米";
                    exAgent.SetCellValue(11, 26, fzp + src);
                    fzp = Convert.ToDouble(dwWASTAMM["WAFBRTU"].ToString().Trim()) * 0.1;
                    src = "CM";
                    exAgent.SetCellValue(11, 5, fzp + src);
                }
                DataTable tbVAART = dsSource.Tables["VAART"];
                if (tbVAART.Rows.Count > 0)
                {
                    DataRow dwVAART = tbVAART.Rows[0];
                    src = dwVAART["ARTNAME"].ToString().Trim();
                    exAgent.SetCellValue(9, 15, Helper.GetGB2312Value(src).Trim());
                } 
                DataTable tbSTUELI = dsSource.Tables["STUELI"]; 
           
                src = dwVKKOPF["VKKNEUDAT"].ToString().Trim();
                exAgent.SetCellValue(7, 5,src );
                src = dwVKKOPF["VKKBESTSB"].ToString().Trim();
                exAgent.SetCellValue(5, 26, src);
                src = dwVKKOPF["VKKBESTNR"].ToString().Trim();
                exAgent.SetCellValue(5, 15, src);
                src = dwVKPOS["VKPART"].ToString().Trim();
                exAgent.SetCellValue(9, 5, src);
                src = dwVKPOS["VKPLIEFTERM"].ToString().Trim();
                exAgent.SetCellValue(7, 26, src);
                src = dwVKPOS["VKPKDKOMM"].ToString().Trim();
                exAgent.SetCellValue(5, 5, src);
                src = dwVKPOS["VKPZUSATZ"].ToString().Substring(0,30).Trim(); 
                exAgent.SetCellValue(9, 26, src);
                src = dwKDSTAMM["KDKUNDNA"].ToString().Trim();
                exAgent.SetCellValue(7, 15, Helper.GetGB2312Value(src).Trim()); 
                src = ""; string WS = ""; string WSP = ""; double WSPP = 0;
                string filter = ""; string sort = ""; string txt = "";
                foreach (DataRow dwST in tbSTUELI.Rows)
                {
                    WS=dwST["STUELISASB"].ToString().Trim();
                    WSP=dwST["STUELIDATEN"].ToString().Trim();
                    WSP = WSP.Substring(0, 4);
                    WSP=WSP.TrimStart('0');
                    WSPP = Convert.ToDouble(WSP) * 0.1;
                    src += WS + WSPP; 
                }
                exAgent.SetCellValue(13, 5,  src);
                
                #region====备注===
                filter = "TXTKFILLER='91' AND Convert(TXTKATR,'System.String') LIKE '%" + 101 + "'";
                 sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                DataRow[] rowss = dsSource.Tables["TXTKON"].Select(filter, sort);  
               
                if (rowss.Length > 1)
                {
                    txt = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[1]["TXTKTEXT"].ToString().Trim()).PadRight(126);
                }
                exAgent.SetCellValue(13, 15, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                filter = "TXTKFILLER='91' AND Convert(TXTKATR,'System.String') LIKE '%" + 102 + "'";
                sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
               rowss = dsSource.Tables["TXTKON"].Select(filter, sort); 
                if (rowss.Length > 1)
                {
                    txt = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[1]["TXTKTEXT"].ToString().Trim()).PadRight(126);
                }
                exAgent.SetCellValue(15, 26, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                filter = "TXTKFILLER='91' AND Convert(TXTKATR,'System.String') LIKE '%" + 103 + "'";
                sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                rowss = dsSource.Tables["TXTKON"].Select(filter, sort);
                if (rowss.Length > 1)
                {
                    txt = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[1]["TXTKTEXT"].ToString().Trim()).PadRight(126);
                }
                exAgent.SetCellValue(15, 5, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                filter = "TXTKFILLER='91' AND Convert(TXTKATR,'System.String') LIKE '%" + 104 + "'";
                sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                rowss = dsSource.Tables["TXTKON"].Select(filter, sort);
                if (rowss.Length > 1)
                {
                    txt = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[1]["TXTKTEXT"].ToString().Trim()).PadRight(126);
                }
                exAgent.SetCellValue(13, 26, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                filter = "TXTKFILLER='91'";
                sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                rowss = dsSource.Tables["TXTKON"].Select(filter, sort);
                if (rowss.Length > 1)
                {
                    txt = (rowss[8]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[9]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[10]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[11]["TXTKTEXT"].ToString().Trim()).PadRight(126) 
                        ;
                }
                exAgent.SetCellValue(53, 1, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                filter = "TXTKFILLER='91'";
                sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                rowss = dsSource.Tables["TXTKON"].Select(filter, sort);
                if (rowss.Length > 1)
                {
                    txt = (rowss[12]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[13]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[14]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[15]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[16]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[17]["TXTKTEXT"].ToString().Trim()).PadRight(126);
                }
                exAgent.SetCellValue(67, 1, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                filter = "TXTKFILLER='81'";
                sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                rowss = dsSource.Tables["TXTKON"].Select(filter, sort);
                if (rowss.Length > 1)
                {
                    txt = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[1]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[2]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[3]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[4]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[5]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[6]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[7]["TXTKTEXT"].ToString().Trim()).PadRight(126) ;
                }
                exAgent.SetCellValue(28, 19, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                filter = "TXTKFILLER='81'";
                sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                rowss = dsSource.Tables["TXTKON"].Select(filter, sort);
                if (rowss.Length > 1)
                {
                    txt = (rowss[8]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[9]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[10]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[11]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[12]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[13]["TXTKTEXT"].ToString().Trim()).PadRight(126) + System.Environment.NewLine
                        + (rowss[14]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[15]["TXTKTEXT"].ToString().Trim()).PadRight(126) ;
                }
                exAgent.SetCellValue(60, 19, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                filter = "TXTKFILLER='81' AND Convert(TXTKATR,'System.String') LIKE '%" + 109 + "'";
                sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                rowss = dsSource.Tables["TXTKON"].Select(filter, sort);
                if (rowss.Length > 1)
                {
                    txt = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim() + rowss[1]["TXTKTEXT"].ToString().Trim()).PadRight(126);
                }
                exAgent.SetCellValue(53, 19, Helper.GetGB2312Value(txt).Trim());
                txt = "";
                
                #endregion
                int bzqtarx = 28;double vkpQTY=0;
                int bzqtary = 13; 
                foreach (DataRow dwVKP in tbVKPOS.Rows)
                {
                    src = dwVKP["VKPKDARTNR"].ToString().Trim();
                    exAgent.SetCellValue(bzqtarx, 1, src);
                    src = dwVKP["VKPKDFARBNR"].ToString().Trim();
                    exAgent.SetCellValue(bzqtarx, 4, src);
                    src=dwVKP["VKPFARBE"].ToString().Trim();
                    exAgent.SetCellValue(bzqtarx, 7, src);
                    vkpQTY = Convert.ToDouble(dwVKP["VKPMENGE1"].ToString().Trim())*0.01;
                    exAgent.SetCellValue(bzqtarx, 9, vkpQTY);
                    src = dwVKP["VKPZUSATZ"].ToString().Substring(30).Trim();
                    exAgent.SetCellValue(bzqtarx, 11, src);
                    
                    #region===边字数量=
                     string sind = (dwVKP["VKPAUFKRS"].ToString() + dwVKP["VKPAUFLFD"].ToString() + dwVKP["VKPAUFPOS"].ToString()+ "VKT").PadRight(60);
                  
                    string qtya = "0"; string qtyb = "0"; string qtyc = "0";
                    filter = "TXTKFILLER='71' AND TXTISIND='" + sind + "' AND TXTKATR='101'";
                    sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                    rowss = dsSource.Tables["TXTKON"].Select(filter, sort);
                   
                    if (rowss.Length > 1)
                    {
                        qtya = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim() );
                    }
                    exAgent.SetCellValue(bzqtarx, bzqtary, qtya);
                    filter = "TXTKFILLER='71' AND TXTISIND='" + sind + "' AND TXTKATR='102'";
                    sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                    rowss = dsSource.Tables["TXTKON"].Select(filter, sort);

                    if (rowss.Length > 1)
                    {
                        qtyb = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim());
                    }
                    exAgent.SetCellValue(bzqtarx, bzqtary + 2, qtyb);
                    filter = "TXTKFILLER='71' AND TXTISIND='" + sind + "' AND TXTKATR='103'";
                    sort = " TXTKPIND ASC,TXTKPOS ASC,TXTKATR ASC";
                    rowss = dsSource.Tables["TXTKON"].Select(filter, sort);

                    if (rowss.Length > 1)
                    {
                        qtyc = (rowss[0]["TXTKTEXT"].ToString().Substring(20).Trim() );
                    }
                    exAgent.SetCellValue(bzqtarx, bzqtary + 4, qtyc);
                     #endregion
                    bzqtarx++;                
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
        }

    }
}
